package com.info.provider.service.aliyun.oos;

import com.aliyun.oss.OSSClient;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.InputStream;

/**
 * Created by pengshuo on 2018/4/11 16:37
 * Description: aliyun oss util
 * Modified By:
 * Version:
 *
 * @author pengshuo
 */
@Service
public class OSSUtils {

    private Logger logger = LogManager.getLogger(this.getClass());

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Autowired
    private OSSClient ossClient;

    /**
     * 储存 oss 文件对象（已指定 bucketName）
     * @param key 文件名称
     * @param input 文件流
     */
    public void store(String key,InputStream input){
        try {
            ossClient.putObject(bucketName,key,input);
        }catch (Exception e){
            logger.error("储存 oss 文件对象 error {}",e);
        }
    }

    /**
     * 删除 oss 文件对象（已指定 bucketName）
     * @param key 文件名称
     */
    public void delete(String key){
        try {
            ossClient.deleteObject(bucketName,key);
        }catch (Exception e){
            logger.error("删除 oss 文件对象 error {}",e);
        }
    }

}
